import 'package:flutter/material.dart';
import 'package:flutter_popcorntime/constants/constants.dart';

import 'package:scoped_model/scoped_model.dart';

import 'package:flutter_popcorntime/models/video_model.dart';
import 'package:flutter_popcorntime/widgets/video_list_widget.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  List tabs = [
    Constants.kTabMovies,
    Constants.kTabSeries,
    Constants.kTabAnime,
  ];

  late VideoModel _videoModel;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);

    _tabController.addListener(() {
      _videoModel.refreshList(tabs[_tabController.index]);
    });

    _videoModel = VideoModel();
    _videoModel.refreshMoviesList();
  }

  @override
  Widget build(BuildContext context) {
    print("HomePage build");
    return ScopedModel<VideoModel>(
      model: _videoModel,
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(50),
          child: AppBar(
            bottom: TabBar(
              controller: _tabController,
              tabs: tabs.map((e) => Tab(text: e)).toList(),
            ),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: tabs
              .map((e) => Container(
                    color: Color.fromRGBO(23, 24, 27, 1.0),
                    child: VideoListWidget(e),
                  ))
              .toList(),
        ),
      ),
    );
  }
}
